package com.meals.system.service.impl;

import com.meals.system.api.user.vo.WhitesMealUserAddressVO;
import com.meals.system.common.ServiceResultEnum;
import com.meals.system.common.SystemException;
import com.meals.system.dao.SystemUserAddressMapper;
import com.meals.system.entity.SystemUserAddress;
import com.meals.system.service.WhitesMealUserAddressService;
import com.meals.system.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/11
 */
@Service
public class WhitesMealUserAddressServiceImpl implements WhitesMealUserAddressService {

    @Autowired
    private SystemUserAddressMapper systemUserAddressMapper;

    @Override
    public List<WhitesMealUserAddressVO> getLoginSystemUserAddress(Long userId) {
        List<SystemUserAddress> systemUserAddressList = systemUserAddressMapper.findAddressList(userId);
        List<WhitesMealUserAddressVO> whitesMealUserAddressVOS = BeanUtil.copyList(systemUserAddressList,WhitesMealUserAddressVO.class);
        return whitesMealUserAddressVOS;
    }

    @Override
    public Boolean insertUserAddress(SystemUserAddress systemUserAddress) {
        Date now  = new Date();
        if (systemUserAddress.getDefaultFlag().intValue() == 1){
            // 添加默认地址，则先把原来的默认地址给取消默认，再添加
            SystemUserAddress defaultAddress = systemUserAddressMapper.getMyDefaultAddress(systemUserAddress.getUserId());
            if (defaultAddress != null){
                defaultAddress.setDefaultFlag((byte) 0);
                defaultAddress.setUpdateTime(now);
                int updateResult = systemUserAddressMapper.updateByPrimaryKeySelective(defaultAddress);
                if (updateResult < 1){
                    SystemException.fail(ServiceResultEnum.DB_ERROR.getResult());
                }
            }

        }
        return systemUserAddressMapper.insertSelective(systemUserAddress) > 0;
    }

    @Override
    public SystemUserAddress getSystemUserAddressById(Long addressId) {
       SystemUserAddress systemUserAddress = systemUserAddressMapper.selectByPrimaryKey(addressId);
       if (systemUserAddress == null){
           SystemException.fail(ServiceResultEnum.DATA_NOT_EXIST.getResult());
       }
       return systemUserAddress;
    }

    @Override
    public Boolean updateSystemUserAddress(SystemUserAddress updateSystemUserAddress) {
        // 找到数据库里对应要更新的地址
        SystemUserAddress tempAddress = getSystemUserAddressById(updateSystemUserAddress.getAddressId());
        Date now = new Date();
        // 修改的地址为新的默认地址，需要将旧的默认地址设为非默认
        if (updateSystemUserAddress.getDefaultFlag().intValue() == 1){
            SystemUserAddress defaultAddress = systemUserAddressMapper.getMyDefaultAddress(updateSystemUserAddress.getUserId());
            // 原默认地址非空，且原默认地址与新的要修改的默认地址不同
            if (defaultAddress != null && !defaultAddress.getAddressId().equals(tempAddress)){
                defaultAddress.setDefaultFlag((byte) 0);
                defaultAddress.setUpdateTime(now);
                int updateResult = systemUserAddressMapper.updateByPrimaryKeySelective(defaultAddress);
                if (updateResult < 1){
                    SystemException.fail(ServiceResultEnum.DB_ERROR.getResult());
                }
            }
        }
        updateSystemUserAddress.setUpdateTime(now);
        return systemUserAddressMapper.updateByPrimaryKeySelective(updateSystemUserAddress) > 0;

    }

    @Override
    public SystemUserAddress getDefaultAddressByUserId(Long userId) {
        SystemUserAddress systemUserAddress = systemUserAddressMapper.getMyDefaultAddress(userId);
        return systemUserAddress;
    }

    @Override
    public Boolean deleteById(Long addressId) {
        return systemUserAddressMapper.deleteByPrimaryKey(addressId) > 0;
    }
}
